import * as echarts from 'echarts';
type EChartsOption = echarts.EChartsOption;
import axios from 'axios'


export default async (myChart?: echarts.ECharts) => {
    myChart&&myChart.showLoading({ text: "",color: "#8870eb" })
    const {data:_rawData} = await axios.get("/echarts/examples/data/asset/data/life-expectancy-table.json")
    myChart&&myChart.hideLoading()
    const countries = [
        'Finland',
        'France',
        'Germany',
        'Iceland',
        'Norway',
        'Poland',
        'Russia',
        'United Kingdom'
    ];
    const datasetWithFilters: echarts.DatasetComponentOption[] = [];
    const seriesList: echarts.SeriesOption[] = [];
    echarts.util.each(countries, function (country) {
        var datasetId = 'dataset_' + country;
        datasetWithFilters.push({
            id: datasetId,
            fromDatasetId: 'dataset_raw',
            transform: {
                type: 'filter',
                config: {
                    and: [
                        { dimension: 'Year', gte: 1950 },
                        { dimension: 'Country', '=': country }
                    ]
                }
            }
        });
        seriesList.push({
            type: 'line',
            datasetId: datasetId,
            showSymbol: false,
            name: country,
            
            // endLabel: {
            //     show: true,
            //     formatter: function (params: any) {
            //         return params.value[3] + ': ' + params.value[0];
            //     }
            // },
            labelLayout: {
                moveOverlap: 'shiftY'
            },
            emphasis: {
                focus: 'series'
            },
            encode: {
                x: 'Year',
                y: 'Income',
                label: ['Country', 'Income'],
                itemName: 'Year',
                tooltip: ['Income']
            },
        });
    });
    
    return {
        animationDuration: 5000,
        dataset: [
            {
                id: 'dataset_raw',
                source: _rawData
            },
            ...datasetWithFilters
        ],
        title: {
            text: 'Line Chart5',
            subtext: 'Fake Data',
            left: 'center'
        },
        tooltip: {
            order: 'valueDesc',
            trigger: 'axis',
           
        },
        toolbox: {
            feature: {
                saveAsImage: {}
            }
        },
        xAxis: {
            type: 'category',
            nameLocation: 'middle'
        },
        yAxis: {
            name: 'Income'
        },
        //调整折线图位置
        grid: {
            right: 20
          },
        series: seriesList
    } as EChartsOption;
}

